#include<allegro.h>
#include "menu.h"

void load_menu(menu* menu)
{
    menu->jouer=load_bitmap("/home/chedly/photo/jouer.bmp",NULL);
    menu->parametre=load_bitmap("/home/chedly/photo/parametre.bmp",NULL);
    menu->aide=load_bitmap("/home/chedly/photo/aide.bmp",NULL);
    menu->quitter=load_bitmap("/home/chedly/photo/quitter.bmp",NULL);
    menu->continer=load_bitmap("/home/chedly/photo/continer.bmp",NULL);
    menu->nouvellepartie=load_bitmap("/home/chedly/photo/nouvellepartie.bmp",NULL);
    menu->score=load_bitmap("/home/chedly/photo/score.bmp",NULL);
    menu->son=load_bitmap("/home/chedly/photo/son.bmp",NULL);

 }
 void contol_menu(menu*menu)
 {


int volume=255;
int menu1=1,menu2=0 ,menu3=0,menu4=0;
SAMPLE *Music=load_sample ("");
play_sample(Music,volume,1000,1000,1);

    BITMAP * Buffer;
    Buffer = create_bitmap(800, 600);

    if (menu->jouer == NULL && menu->parametre== NULL && menu->aide== NULL && menu->quitter== NULL&& menu->continer== NULL&& menu->nouvellepartie== NULL && menu->score== NULL && menu->son== NULL)
    {
        allegro_message("could not load images");
        allegro_exit();
    }
    draw_sprite(Buffer, menu->jouer, 0, 0);
	int joueer=0;

    while (!key[KEY_ESC] && joueer!=1)
    {


if (menu1==1)
{

        if (key [KEY_UP])

    	{
        	    draw_sprite(Buffer,menu->jouer, 0, 0);
    		readkey();
	         if (key [KEY_ENTER])
        	{

            		draw_sprite(Buffer,menu->nouvellepartie , 0, 0);
			menu1=0;
			menu2=1;
    		}

        }

}
     if (menu1==1)
{

        if (key [KEY_RIGHT])

    	{
        	    draw_sprite(Buffer, menu->parametre, 0, 0);
    		readkey();
	         if (key [KEY_ENTER])
        	{

            		draw_sprite(Buffer, menu->son , 0, 0);

			menu1=0;
			menu3=1;
    		}

        }


}


           if (menu1==1)
{

                 if (key [KEY_LEFT])


    	{
        	    draw_sprite(Buffer, menu->aide, 0, 0);
    		readkey();
	         if (key [KEY_ENTER])
        	{

            		draw_sprite(Buffer, menu->nouvellepartie , 0, 0);
			menu1=0;
			menu4=1;
    		}

        }

}


          if (key [KEY_DOWN])
        {
            draw_sprite(Buffer, menu->quitter, 0, 0);

readkey();
	         if (key [KEY_ENTER])
        	{

            		exit(0);
    		}

        }

if (menu2==1)
{
    if (key [KEY_UP])

    	{
        	    draw_sprite(Buffer, menu->continer, 0, 0);
        }
        if (key [KEY_DOWN])

    	{
        	    draw_sprite(Buffer, menu->nouvellepartie, 0, 0);
        }
        if (key[KEY_BACKSPACE])
        {
            menu2=0;
            menu1=1;
           draw_sprite(Buffer, menu->jouer, 0, 0);

        }
}
if (menu3==1)
{
    if (key [KEY_UP])

    	{
        	    draw_sprite(Buffer, menu->son, 0, 0);
        }
        if (key [KEY_DOWN])

    	{
        	    draw_sprite(Buffer, menu->score, 0, 0);
        }

if (key [KEY_RIGHT])

    	{
    	    if (volume<255)
    	    volume++;
    	    adjust_sample(Music,volume,1000,1000,1);

        	    draw_sprite(Buffer, menu->son, 0, 0);
        }
        if (key [KEY_LEFT])

    	{
    	    if (volume>0)
    	    volume--;
    	    adjust_sample(Music,volume,1000,1000,1);

        	    draw_sprite(Buffer, menu->son, 0, 0);
        }

        if (key[KEY_BACKSPACE])
        {
            menu3=0;
            menu1=1;
           draw_sprite(Buffer, menu->jouer, 0, 0);

        }
}
if (menu4==1)
{



        	    draw_sprite(Buffer, menu->nouvellepartie, 0, 0);

        if (key[KEY_BACKSPACE])
        {
            menu4=0;
            menu1=1;
           draw_sprite(Buffer, menu->jouer, 0, 0);

        }
}
    blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}
destroy_sample(Music);

}

void destroy_menu(menu *menu)

 {

    destroy_bitmap( menu->jouer);
    destroy_bitmap(menu->parametre);
    destroy_bitmap(menu->aide);
    destroy_bitmap(menu->quitter);
    destroy_bitmap(menu->continer);
    destroy_bitmap(menu->nouvellepartie);
    destroy_bitmap(menu->score);
    destroy_bitmap(menu->son);

    }
